package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinacoal.hr.levelapi.entity.TransPaperDetailList;
import com.chinacoal.hr.levelimpl.mapper.TransPaperDetailListMapper;
import com.chinacoal.hr.levelimpl.utils.StaticDicts;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-17 13:55
 * @description: 员工事务单汇总表Dao
 **/
@Repository
public class TransPaperDetailListDao {
    @Resource
    private TransPaperDetailListMapper transPaperDetailListMapper;

    public void insertTransPaperDetailList(TransPaperDetailList record) throws Throwable {
        transPaperDetailListMapper.insert(record);

    }

    public void updateTransPaperDetailList(TransPaperDetailList record) {
        transPaperDetailListMapper.updateById(record);
    }

    /**
     * 根据模板update实体
     *
     * @param entity   :
     * @param template :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    public void updateTransPaperDetailListByTemplate(TransPaperDetailList template, TransPaperDetailList entity) {
        String paDocId = template.getPaDocId();

        QueryWrapper wrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(paDocId)) {
            wrapper.eq("PA_DOC_ID", paDocId);
        }


        transPaperDetailListMapper.update(entity, wrapper);
    }

    /**
     * 根据条件模板删除员工事务单汇总表
     *
     * @param condition :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/4 2019/12/4
     */
    public void deleteTransPaperDetailListByCondition(Map<String, Object> condition) throws Throwable {
        try {
            QueryWrapper queryWrapper = new QueryWrapper();

            String paDocId = condition.get("paDocId") != null ? condition.get("paDocId").toString() : "";

            if (StringUtils.isNotBlank(paDocId)) {
                queryWrapper.eq("PA_DOC_ID", paDocId);
            }

            this.transPaperDetailListMapper.delete(queryWrapper);
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 按条件查询 员工事务单汇总表
     *
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.TransPaperDetailList>
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    public List<TransPaperDetailList> getTransPaperDetailList(Map condition) throws Throwable {
//        condition.put("bizStatus", StaticDicts.BUSINESS_STATUS_APPROVALED);
//        condition.put("effectFlag", StaticDicts.EFFECTFLAG_INVALID);
        String beginDate = condition.get("beginDate") == null ? "" : condition.get("beginDate").toString();
        String endDate = condition.get("endDate") == null ? "" : condition.get("endDate").toString();

        String bizStatus = condition.get("bizStatus") == null ? "" : condition.get("bizStatus").toString();
        String effectFlag = condition.get("effectFlag") == null ? "" : condition.get("effectFlag").toString();
        String empCode = condition.get("empCode") == null ? "" : condition.get("empCode").toString();

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        if (StringUtils.isNotBlank(bizStatus)) {
            queryWrapper.eq("BUSINESS_STATUS", bizStatus);
        }

        if (StringUtils.isNotBlank(effectFlag)) {
            queryWrapper.eq("EFFECT_FLAG", effectFlag);
        }

        /**
         * 时间查询条件规则
         * 查询条件开始时间小于数据库开始时间字段，或查询条件结束时间大于数据库结束时间字段
         */
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            String dateQuerySql = "((UNIX_TIMESTAMP('" + beginDate + " 00:00:00') <= UNIX_TIMESTAMP(PA_DATE) " +
                    ") " +
                    "or " +
                    "(" +
                    "UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(PA_DATE)))";
            queryWrapper.apply(dateQuerySql);
        }

        queryWrapper.orderByDesc("LAST_UPDATE_DATE");

        List<TransPaperDetailList> transPaperDetailLists = this.transPaperDetailListMapper.selectList(queryWrapper);
        return transPaperDetailLists;
    }

}
